package com.ohos.carbon.slice.widgets;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.element.ElementScatter;

import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;
public class BottomSheetSlice extends AbilitySlice {

    private Image back_button;
    private Text text_title,textView,textList,textGrit;
    private DirectionalLayout dir,dirLayout,directMenut,dirMenuLayout;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_bottom_sheet);
        initView();
        setClick();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0){
            if (MainAbilitySlice.all.containsKey("BottomSheet")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("BottomSheet")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("BottomSheet", integer);
                        pf.flushSync();
                    }
                }
            }else {
                pf.putInt("BottomSheet", 1);
                pf.flushSync();
            }
        }else {
            pf.putInt("BottomSheet", 1);
            pf.flushSync();
        }
    }

    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        text_title.setText("BottomSheet");
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        //初始化
        dir = (DirectionalLayout)findComponentById(ResourceTable.Id_dir);
        textView = (Text)findComponentById(ResourceTable.Id_text_view);
        dirLayout = (DirectionalLayout)findComponentById(ResourceTable.Id_dir_layout);
        textList = (Text)findComponentById(ResourceTable.Id_text_list);
        textGrit = (Text)findComponentById(ResourceTable.Id_text_grit);
        directMenut = (DirectionalLayout)findComponentById(ResourceTable.Id_direct_menu);
        dirMenuLayout = (DirectionalLayout)findComponentById(ResourceTable.Id_dir_menu_layout);
        back_button.setClickedListener(component -> onBackPressed());
    }

    private void setClick() {
        dir.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                dir.setVisibility(Component.INVISIBLE);
                dirLayout.setVisibility(Component.VISIBLE);
            }
        });

        textList.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                dirLayout.setVisibility(Component.HIDE);
                textView.setText("List");
                dir.setVisibility(Component.VISIBLE);
                directMenut.setVisibility(Component.VISIBLE);
                dirMenuLayout.setVisibility(Component.HIDE);
            }
        });

        textGrit.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                dirLayout.setVisibility(Component.HIDE);
                textView.setText("Grid");
                dir.setVisibility(Component.VISIBLE);
                directMenut.setVisibility(Component.HIDE);
                dirMenuLayout.setVisibility(Component.VISIBLE);
            }
        });

    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
